/**
* Created by wl
* 项目管理>持有中-详情页-财务数据-预警配置
*/
<template>
    <div class="ca-box ca-m-t-28 ca-p-20">
        <el-button type="primary" class="el-right" @click="checkInfo('')" :disabled="!isAuthIndexSee">创建预警</el-button>
        <div class="ca-m-t-28">
            <table class="ca-table ca-m-t-15">
                <tr>
                    <th width='20%'>预警规则名称</th>
                    <th width='35%'>预警阈值</th>
                    <th width='15%'>状态</th>
                    <th width='10%'>启用操作</th>
                    <th width='10%'>操作</th>
                </tr>
                <tr v-for="(item, index) in remindList" :key="index">
                    <td @click="checkInfo(item.id)" class="addBlue">{{item.title}}</td>
                    <td>
                        <span v-if="item.threshold1 != null">告警：{{item.remindKey}}{{alarmDate[item.contrastiveValue].name}}{{alarmType[item.contrastiveType]}}
                            <span v-if='item.threshold11'>{{item.symbol1}}{{item.threshold1}}--{{item.threshold11}}</span>
                            <span v-else>{{item.symbol1}}{{item.threshold1}}</span></br>
                        </span>
                        <span v-if="item.threshold2 != null">关注：{{item.remindKey}}{{alarmDate[item.contrastiveValue].name}}{{alarmType[item.contrastiveType]}}
                        <span v-if='item.threshold22'>{{item.symbol2}}{{item.threshold2}}--{{item.threshold22}}</span>
                        <span v-else>{{item.symbol2}}{{item.threshold2}}</span>
                        </span>
                    </td>
                    <td :class="{red: item.remindStatus==0}">{{item.remindStatus == 0 ? '已停用' : '已启用' }}</td>
                    <td>
                        
                        <el-button type="text" @click="changeRemind(1,item.id)" class="red" v-if="item.remindStatus == 0">启用</el-button>
                        <el-button type="text" @click="changeRemind(0,item.id)" class="black" v-if="item.remindStatus == 1">停用</el-button>
                    </td>
                    <td class="ca-pointer" @click="delRemind(item.id)">删除</td>
                </tr>
            </table>
            <div class='el-center'>
                <el-button @click="closeAlarm">取消</el-button>
                <el-button type="primary" @click="closeAlarm">确定</el-button>
            </div>
        </div>
        <csAlarmConfigure v-if="dialogVisible" :dialogVisible='dialogVisible' @close="close" :remindId='remindId' @save="save"></csAlarmConfigure>
    </div>
</template>
<script type="text/ecmascript-6">
    import {api} from '../../api';
    import {STATUS_HTTP_SUCCESS, apiHttp} from '__service/csHttp';
    import { addRemind, isNoAuth } from '__service/utils';
    export default {
        data() {
            return {
                isAuthIndexSee: isNoAuth('持有中项目-财务数据-编辑'),
                dialogVisible: false,
                remindList: [],
                remindId: '',
                alarmType: ['比值', '环比', '同比'],
                alarmDate: [
                    {
                        name: '按年',
                        id: 0,
                    },
                    {
                        name: '半年',
                        id: 1,
                    },
                    {
                        name: '按季度',
                        id: 2,
                    },
                ],
            }
        },
        watch: {
        },
        props: {
        },
        mounted() {
            this.getList()
        },
        methods: {
            closeAlarm() {
                this.$emit('close')
            },
            close() {
                this.dialogVisible = false
            },
            async save() {
                this.dialogVisible = false
                await this.getList();
                // 添加小红点
                await addRemind({
                    dataId: this.$route.params.itemId, // 业务id
                    dataTab: 3, // tab页顺序 head 也为0 新建为'-1'
                    dataType: 2, // 1拟投资项目,2已投资项目,3母基金,4拟设立基金,5已设立基金,6客户,7潜在项目
                });
                // 根据第三方组件来调用 父组件中定义的方法 重新获取小红点
                this.$root.Dot.$emit('getDotList');
                // 添加小红点 end
            },
            getList() {
                apiHttp(api.CHECK_REMIND, {itemId: this.$route.params.itemId}).then(ret => {
                    if (ret.resCode === STATUS_HTTP_SUCCESS) {
                        this.remindList = ret.data
                    }
                })
            },
            delRemind(id) {
                let that = this
                this.$confirm('确定要删除此信息吗?', '提示', {
                    cancelButtonText: '取消',
                    confirmButtonText: '确定',
                    type: 'warning'
                }).then(function () {
                    //删除操作
                    apiHttp(api.DEL_REMIND, {itemId: that.$route.params.itemId, remindId: id}).then(ret => {
                        if (ret.resCode === STATUS_HTTP_SUCCESS) {
                            that.$message({
                                message: '数据删除成功！',
                                type: 'success'
                            });
                            that.save()
                        }
                    })
                })
            },
            checkInfo(id) {
                this.remindId = id
                this.dialogVisible = true
            },
            changeRemind(status, id) {
                let that = this
                this.$confirm('确定要更改状态吗?', '提示', {
                    cancelButtonText: '取消',
                    confirmButtonText: '确定',
                    type: 'warning'
                }).then(function () {
                    let query = {
                        itemId: that.$route.params.itemId,
                        remindId: id,
                        remindStatus: status,
                    }
                    // // 初始化
                    // // api.CHANGE_REMIND.url = '/hold/item/{itemId}/finance/remind/${remindId}'
                    // // url拼接
                    // api.CHANGE_REMIND.url = api.CHANGE_REMIND.url + '?remindStatus=' + status
                    apiHttp(api.CHANGE_REMIND, query).then(ret => {
                        if (ret.resCode === STATUS_HTTP_SUCCESS) {
                            that.$message({
                                message: '数据修改成功！',
                                type: 'success'
                            });
                            that.save()
                        }
                    })
                })
            },
        },
        components: {
            csAlarmConfigure: require('./edit'),
        },
    }
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
.el-right{
    float: right;
    margin-bottom: 10px;
}
.el-center{
    text-align: center;
    margin-top: 10px;
}
.ca-pointer {
    cursor: pointer;
    color: red;
}
.red {
    color: red;
}
.black {
    color: #000;
}
.addBlue {
    color: blue;
    text-decoration: underline;
    cursor: pointer;
}
</style>